package fr.labri.harmony.analysis.jsep.results.filter;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import fr.labri.harmony.analysis.jsep.results.Pair;


public class Utils {
	
	public static List<Pair<Long,Long>> dates=null;
	
	public static String formatDouble(double s) {
		return new DecimalFormat("#.##").format(s);
	}

	public static String formatDate(long ts) {
		if(System.getProperty("user.language").toString().equals("fr")) {
			DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
			return dateFormat.format(new Date(ts));
		}
		else {
			DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			return dateFormat.format(new Date(ts));
		}
	}
	
	public static List<Pair<Long,Long>> generateDates() {
		if(dates!=null)
			return dates;
		dates = new ArrayList<Pair<Long,Long>>();
		long now = System.currentTimeMillis();
		Calendar nowCal = Calendar.getInstance();
		nowCal.setTime(new Date(now));
		for(int i = 0 ; i < 10 ; i++) {
			Calendar prev = Calendar.getInstance();
			prev.set(nowCal.get(Calendar.YEAR)-1, nowCal.get(Calendar.MONTH), nowCal.get(Calendar.DAY_OF_MONTH));
			dates.add(0, new Pair<Long,Long>(prev.getTimeInMillis(),nowCal.getTimeInMillis()));
			nowCal = prev;
		}
		return dates;
	}
	
	public static int getPeriod(long timestamp) {
		if(dates==null)
			dates = generateDates();
		int k=0;
		for(Pair<Long,Long> period : dates) {
			if(timestamp > period.getElement0() && timestamp < period.getElement1())
				return k;
			k++;
		}
		return -1;
	}

	public static int getHoursDiff(long start, long end) {
		long diff = end-start;
		return (int) (diff/(60 * 60 * 1000)); // hours
	}
	
	public static String optimizeString(String message, String source, String target) {
		
		String msg = message.toLowerCase();
		int idxa = msg.indexOf(source);
		int idxb = msg.indexOf(target);

		int idxmin = 0;
		int idxmax = message.length();
		List<Integer> indexes = new ArrayList<Integer>();
		
		String log = ""; 
		int delimit = 300;
		while(idxa!=-1 || idxb!=-1) {
			System.out.println(idxa+"\t"+idxb);
			if(idxa !=-1) {
				indexes.add(idxa-delimit);
				indexes.add(idxa+delimit);
				idxa = msg.indexOf(source,idxa+1);
			}
			else {
				idxa = msg.indexOf(source.replaceAll("\\-", " "));
				if(idxa !=-1) {
					indexes.add(idxa-delimit);
					indexes.add(idxa+delimit);
					idxa = msg.indexOf(source.replaceAll("\\-", " "),idxa+1);
				}
			}

			if(idxb !=-1) {
				indexes.add(idxb-delimit);
				indexes.add(idxb+delimit);
				idxb = msg.indexOf(target,idxb+1);
			}
			else {
				idxb = msg.indexOf(target.replaceAll("\\-", " "));
				if(idxb !=-1) {
					indexes.add(idxb - delimit);
					indexes.add(idxb + delimit);
					idxb = msg.indexOf(target.replaceAll("\\-", " "),idxb+1);
				}
			}
		}
		if(!indexes.isEmpty()) {
			Collections.sort(indexes);
			idxmin = indexes.get(0);
			if(idxmin < 0) idxmin = 0;
			idxmax = indexes.get(indexes.size()-1);
			if(idxmax >= message.length()) idxmax = message.length();
			log=message.substring(idxmin, idxmax);
		}
		
		return log;
	}
	
	static Set<String> filterLibs(String[] array) {
		Set<String> libs = new HashSet<String>(Arrays.asList(array));
		libs.remove("omero-common");
		libs.remove("ioncore-java");
		libs.remove("jdbc");
		libs.remove("serial");
		libs.remove("play");
		if(libs.contains("mysql-connector-java")) {
			libs.remove("mysql-connector-java");
			libs.add("mysql");
		}
		libs.remove("");
		libs.remove("lucene-xercesImpl");
		return libs;
	}
	
}
